<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { equipApi } from '@/api/controller';
  import { ToastSuccess, ToastWarning, Toastloading } from '@/utils/public';
  import { ElMessageBox } from 'element-plus';
  const formLabelWidth = '120px'
  import updateConfig from './updateConfig.vue'
  import RewardList from './RewardList.vue'
  const updateConfigRef = ref(),
  RewardListRef = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    game_id:0,
    list:[] as Array<any>
  })
  const listQuery = reactive({
    white_id:'',
  })
  const openForm = (row:any = {})=>{
    updateConfigRef.value.openDialog({
      white_id:listQuery.white_id,
      game_id:action.game_id
    })
  }
  const openReward = (row:any = {})=>{
    RewardListRef.value.openDialog(row)
  }
  const handleDelete = (row:any = {})=>{
    const { id=0 } = row
    if(!id) return ToastWarning('缺少id')
    ElMessageBox.confirm("确定删除此白名单配置吗?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      const loading = Toastloading();
      equipApi.whiteConfigDelete({id}).then(()=>{
        ToastSuccess("删除成功!");
        loading.close();
        getList();
      }).catch(()=>{
        loading.close();
      })
    }).catch(() => {});
  }
  const getList = async()=>{
    const loading = Toastloading()
    const res = await equipApi.whiteConfigList(listQuery)
    loading.close()
    const { data=[] } = res || {}
    action.list = data
  }
  const openDialog = async(query:any = {})=> {
    const { id=0, gameList=[], game_id=0 } = query
    action.game_id = game_id
    listQuery.white_id = id
    await getList()
    action.dialogFormVisible = true
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    width="70%"
    title="白名单配置">
      <div class="xm-headTab">
        <div class="labelBox">
          <div class="labelItem">
            <el-button type="primary" @click="openForm">新增</el-button>
          </div>
        </div>
      </div>
      <el-table
        class="xm-main xm-TableCenter"
        :data="action.list"
        border>
        <el-table-column width="120" prop="id" label="ID" />
        <el-table-column width="120" prop="game_id" label="游戏ID" />
        <el-table-column min-width="120" prop="gameName" label="游戏名称" />
        <el-table-column min-width="140" prop="white_id" label="白名单ID" />
        <el-table-column min-width="140" prop="content" label="描述" />
        <el-table-column min-width="140" prop="num_st" label="次数起始" />
        <el-table-column min-width="140" prop="num_ed" label="次数截止" />
        <el-table-column min-width="140" prop="white_reward" label="产出信息">
          <template #default="scope">
            <el-button type="primary" @click="openReward(scope.row)">查看</el-button>
          </template>
        </el-table-column>
        <el-table-column width="120" fixed=right label="操作">
          <template #default="scope">
            <el-button type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <updateConfig ref="updateConfigRef" @updateList="getList" />
    <RewardList ref="RewardListRef" />
  </div>
</template>